package org.py.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.py.common.MessageCode;
import org.py.common.MessageObject;
import org.py.common.MessageUtils;
import org.py.entity.Music;
import org.py.mapper.MusicMapper;
import org.py.service.IMusicService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author anyi
 * @since 2025-03-14
 */
@Service
public class MusicServiceImpl extends ServiceImpl<MusicMapper, Music> implements IMusicService {
    @Autowired
    private MusicMapper musicMapper;


    @Override
    public String getAllUsers(String json) {
        MessageObject messageObject = MessageUtils.ObectToClass(json, Music.class);
        Music musicParam = (Music) messageObject.getContent();
        String musickeys = musicParam.getMusickeys();
        String name = musicParam.getName();
        if (ObjectUtils.isEmpty(musickeys)){
            messageObject.setCode(MessageCode.M1001);
            messageObject.setContent("请给正确参数");
            return MessageUtils.ObjectTojson(messageObject);
        }

        QueryWrapper<Music> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(name)){
            queryWrapper.like("name", name);
        }
        queryWrapper.eq("musickeys", musicParam.getMusickeys());
        queryWrapper.select(
                "id",
                "name",
                "musickeys",
                "create_time",
                "update_time",
                "isaudit"
        );
        long pagesize = messageObject.getPagesize();
        long current = messageObject.getCurrent();
        Page<Music>page = new Page<>(pagesize, current);
        Page<Music> musicPage = musicMapper.selectPage(page, queryWrapper);
        List<Music> records = musicPage.getRecords();
        messageObject.setCode(MessageCode.M1000);
        messageObject.setPages(musicPage.getTotal());
        messageObject.setContent(records);
        return MessageUtils.ObjectTojson(messageObject);
    }


    @Override
    public String getUserById(String json) {
        MessageObject messageObject = MessageUtils.ObectToClass(json, Music.class);
        Music music = (Music) messageObject.getContent();
        QueryWrapper<Music> queryWrapper=new QueryWrapper<>();
        if (ObjectUtils.isNotEmpty(music)){
            queryWrapper.eq("id",music.getId());
        }
        queryWrapper.select(
                "id",
                "spectrum"
        );
        Music music1 = musicMapper.selectOne(queryWrapper);
        messageObject.setCode(MessageCode.M1000);
        messageObject.setContent(music1);
        return MessageUtils.ObjectTojson(messageObject);
    }

    @Override
    public String createUser(String json) {
        MessageObject messageObject = MessageUtils.ObectToClass(json, Music.class);
        Music music = (Music) messageObject.getContent();
        int insert = musicMapper.insert(music);
        if (insert<0){
            messageObject.setCode(MessageCode.M1001);
            messageObject.setContent("新增失败");
            return MessageUtils.ObjectTojson(messageObject);
        }
        messageObject.setCode(MessageCode.M1000);
        messageObject.setContent("新增成功");
        return MessageUtils.ObjectTojson(messageObject);
    }

    @Override
    public String updateUser(String json) {
        MessageObject messageObject = MessageUtils.ObectToClass(json, Music.class);
        Music music = (Music) messageObject.getContent();
        UpdateWrapper<Music> updateWrapper=new UpdateWrapper<>();
        if (ObjectUtils.isNotEmpty(music)){
            updateWrapper.eq("id",music.getId());
            if (StringUtils.isNotEmpty(music.getName())){
                updateWrapper.set("name",music.getName());
            }
            if (StringUtils.isNotEmpty(music.getSpectrum())){
                updateWrapper.set("spectrum",music.getSpectrum());
            }
        }
        int update = musicMapper.update(updateWrapper);
        if (update<0){
            messageObject.setCode(MessageCode.M1001);
            messageObject.setContent("修改失败");
            return MessageUtils.ObjectTojson(messageObject);
        }
        messageObject.setCode(MessageCode.M1000);
        messageObject.setContent("修改成功");
        return MessageUtils.ObjectTojson(messageObject);
    }

    @Override
    public String deleteUser(String json) {
        MessageObject messageObject = MessageUtils.ObectToClass(json, Music.class);
        Music music = (Music) messageObject.getContent();
        if (ObjectUtils.isNotEmpty(music)){
            int delete = musicMapper.deleteById(music.getId());
            if (delete<0){
                messageObject.setCode(MessageCode.M1001);
                messageObject.setContent("删除失败");
                return MessageUtils.ObjectTojson(messageObject);
            }

        }
        messageObject.setCode(MessageCode.M1000);
        messageObject.setContent("删除成功");
        return MessageUtils.ObjectTojson(messageObject);
    }
}
